<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\UsersTransactionsModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\usersTransactions;
use mbilling\BillingBundle\Entity\usersTotalTransactions;
use mbilling\BillingBundle\Entity\usersAdminLogs;

class UsersTransactionsController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $transaction_model = new UsersTransactionsModel($em);

        $transactions = $transaction_model->findAll();
        return $this->render('BillingBundle:UsersTransactions:index.html.twig', array('transactions' => $transactions));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $transaction_model = new UsersTransactionsModel($em);
        $transaction = new usersTransactions();
        $transaction_model->create($transaction->initDefaultEntity());

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('5');//страница транзакции
        $log->setObjectId($transaction->getId());//изменяемый объект
        $log->setActionTypeId('1');//создание транзакции
        $log_model->create($log);
        return new RedirectResponse($this->generateUrl('users_transactions_show'));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $transaction_model = new UsersTransactionsModel($em);
        $transaction_model->removeByPK($id);

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('5');//страница транзакции
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление транзакции
        $log_model->create($log);
        return new RedirectResponse($this->generateUrl('users_transactions_show'));
    }
}